﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class UIFieldRunMap : Page
    {
        public static string fieldId = "";
        public static bool isUIFieldRunMap = false;

        #region popup box
        public UIFieldReadState readState;
        public UIFieldAskPk askPk;
        public UIFieldAskEaster askEaster;
        public UIFieldRunMapCombatLog combatLog;
        public UIFieldRunMapBattleAttack battleAttack;
        public UIFieldRunMapChangeLine changleLine;
        public UIFieldSetHpPage hpSetPage;
        public UIFieldHpSetPanel hpBtnPanel;
        public UIFieldRunMapTeamPanelInfo teamPanel;
        #endregion

        #region gameObject
        public GameObject battleRedTip;
        #endregion

        #region event
        public UIFieldRunMapEvent uiEvent;
        #endregion

        #region info
        public UIFieldRunMapChatInfo chatInfo;
        public UIFieldRunMapFoodInfo foodInfo;
        public UIFieldRunMapHeroInfo heroInfo;
        public UIFieldRunMapTopInfo topInfo;
        public UIFieldRunMapTeamInfo teamInfo;
        public UIFieldRunMapBossInfo bossInfo;
        #endregion

        protected override void DoOpen()
        {
            if (UIFieldRunMap.fieldId != StaticData.fieldId)
            {
                ManagerController.Instance.TownToField();
            }
            Init();
            if(StaticData.townNeedLogin)
            {
                StaticData.townNeedLogin = false;
                if(GuideManager.Instance!=null)
                {
                    GuideManager.Instance.OnLogin(false);
                }
            }

            isUIFieldRunMap = true;
        }

        public override void OnForceClose()
        {
            isUIFieldRunMap = false;
        }
        protected override void DoClose()
        {
        }
        public override void OnCoverPageRemove()
        {
            UIFieldRunMap.RefreshPage();
            UIFieldRunMap.RefreshTeamInfo();
        }

        void Awake()
        {
            readState.gameObject.SetActive(false);
            askPk.gameObject.SetActive(false);
            combatLog.gameObject.SetActive(false);
            battleAttack.gameObject.SetActive(false);
            changleLine.gameObject.SetActive(false);
            askEaster.gameObject.SetActive(false);
            hpSetPage.gameObject.SetActive(false);
            battleRedTip.gameObject.SetActive(false);
            teamInfo.gameObject.SetActive(false);
            teamPanel.gameObject.SetActive(false);
        }

        
        public void Init()
        {
            foodInfo.Init();

            topInfo.Init();
            askPk.page = this;
            readState.page = this;
            battleAttack.page = this;
            combatLog.page = this;
            hpBtnPanel.setHpItems();
            heroInfo.Refresh();
            teamInfo.ReRefresh();
        }
    
     
        public void Refresh()
        {
            foodInfo.Refresh();
            heroInfo.Refresh();
            topInfo.Refresh();
            hpBtnPanel.Refresh();
        }

        #region 工具
        public static void OpenGetResourceWaiting(OkHander ok)
        {
            var page = PageManager.instance.currentPage as UIFieldRunMap;
            if (page != null && page.readState != null)
            {
                page.readState.Init(StaticData.resId,ok);
            }
        }
        
        public static void DisplayGetResource(ResultObject obj)
        {
            CommonMethod.ShowGetObjs(obj);
            StaticData.playerData.AddResultObj(obj);
            Dict.Blo.DictFieldResourceBlo.GetOne(StaticData.resId);
        }

        public static void AskPk(MovePlayer movePlayer, OkHander0 ok0,string tipStr)
        {
            var page = PageManager.Instance.currentPage as UIFieldRunMap;
            if (page != null && page.askPk != null)
            {
                page.askPk.Init(movePlayer, ok0,tipStr);
            }
        }

        public static void BattleReady(SCSidePvpFightResult result,OkHander playAni)
        {
            var page = PageManager.Instance.currentPage as UIFieldRunMap;
            if (page != null)
            {
                page.battleAttack.Init(result,playAni);
            }
        }

        public static void SetSpeed(object msg)
        {
            var page = PageManager.Instance.currentPage as UIFieldRunMap;
            if (page != null && page.topInfo!=null)
            {
                page.topInfo.SetSpeed((SCPingTcp)msg);
            }
        }

        public static void RefreshPage()
        {
            var page = UI.PageManager.Instance.currentPage as UI.UIFieldRunMap;
            if (page != null)
            {
                page.Refresh();
            }
        }

        public static void RefreshTeamInfo()
        {
            var page = UI.PageManager.Instance.currentPage as UI.UIFieldRunMap;
            if (page != null)
            {
                page.teamInfo.ReRefresh();
            }
        }
        
        public static void AskReborn(OkHander freeReborn,OkHander buyReborn)
        {
            var page = UI.PageManager.Instance.currentPage as UI.UIFieldRunMap;
            if(page != null)
            {
                page.askEaster.Init(freeReborn,buyReborn);
            }
        }

        public static void RefreshLineInfo()
        {
            var page = UI.PageManager.Instance.currentPage as UI.UIFieldRunMap;
            if (page != null)
            {
                page.changleLine.Refresh();
            }
        }

        public static void RefreshBossTime(float second)
        {
            var page = UI.PageManager.Instance.currentPage as UI.UIFieldRunMap;
            if(page != null)
            {
                page.bossInfo.ResetTimer(second);
            }
        }
        
        public void OnClickSetHpBtn()
        {
            var page = PageManager.Instance.currentPage as UIFieldRunMap;
            if (page != null && page.hpSetPage != null)
            {
                page.hpSetPage.show(hpBtnPanel);
            }
        }
        
        #endregion
    }
}